package com.hjy;

import cn.hutool.core.util.StrUtil;
import com.hjy.mapper.SqlMapper;
import com.hjy.utils.DataUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.*;

@SpringBootTest
public class test1 {

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    DataUtil dataUtil;

    @Test
    public void test1() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("mapperXml.ftl");

        // 数据
        HashMap<String, Object> module = dataUtil.getTemplateData("auth");
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\auth.xml"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();
    }


    @Test
    public void test2() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("com.hjy.mapper.ftl");
        // 构造数据
        List<Map<String, String>> columnsInfo = sqlMapper.queryColumns("auth");
        Map<String, String> tableInfo = sqlMapper.queryTable("auth");

        // 数据
        HashMap<String, Object> module = new HashMap<>();

        module.put("package","com.hjy");
        module.put("moduleName","");
        module.put("tableName",tableInfo.get("tableName"));
        module.put("tableComment",tableInfo.get("tableComment"));
        module.put("className",StrUtil.upperFirst(tableInfo.get("tableName").toString()));
        List<Map<String, String>> maps = new ArrayList<>();
        HashMap<String, String> pkMap = new HashMap<>();
        for (Map<String, String> map : columnsInfo) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("comment",map.get("columnComment"));
            objectObjectHashMap.put("attrName",map.get("columnName"));
            objectObjectHashMap.put("columnName",map.get("columnName"));
            objectObjectHashMap.put("attrType","String");
            if (map.get("columnKey").equals("PRI")){
                pkMap.put("columnName",map.get("columnName"));
                if (map.get("EXTRA").equals("auto_increment")) {
                    pkMap.put("type","auto_increment");
                }else {
                    pkMap.put("type","");
                }
            }
            maps.add(objectObjectHashMap);
        }
        module.put("columns",maps);
        if (pkMap.size() <= 0) {
            pkMap.put("columnName",maps.get(0).get("tableName"));
            pkMap.put("type","");
        }
        module.put("pk",pkMap);
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\AuthMapper.java"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();


    }

    @Test
    public void test3() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("com.hjy.service.ftl");
        // 构造数据
        List<Map<String, String>> columnsInfo = sqlMapper.queryColumns("auth");
        Map<String, String> tableInfo = sqlMapper.queryTable("auth");

        // 数据
        HashMap<String, Object> module = new HashMap<>();

        module.put("package","com.hjy");
        module.put("moduleName","");
        module.put("tableName",tableInfo.get("tableName"));
        module.put("tableComment",tableInfo.get("tableComment"));
        module.put("className",StrUtil.upperFirst(tableInfo.get("tableName").toString()));
        List<Map<String, String>> maps = new ArrayList<>();
        HashMap<String, String> pkMap = new HashMap<>();
        for (Map<String, String> map : columnsInfo) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("comment",map.get("columnComment"));
            objectObjectHashMap.put("attrName",map.get("columnName"));
            objectObjectHashMap.put("columnName",map.get("columnName"));
            objectObjectHashMap.put("attrType","String");
            if (map.get("columnKey").equals("PRI")){
                pkMap.put("columnName",map.get("columnName"));
                if (map.get("EXTRA").equals("auto_increment")) {
                    pkMap.put("type","auto_increment");
                }else {
                    pkMap.put("type","");
                }
            }
            maps.add(objectObjectHashMap);
        }
        module.put("columns",maps);
        if (pkMap.size() <= 0) {
            pkMap.put("columnName",maps.get(0).get("tableName"));
            pkMap.put("type","");
        }
        module.put("pk",pkMap);
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\AuthService.java"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();


    }

    @Test
    public void test4() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("serviceImpl.ftl");
        // 构造数据
        List<Map<String, String>> columnsInfo = sqlMapper.queryColumns("auth");
        Map<String, String> tableInfo = sqlMapper.queryTable("auth");

        // 数据
        HashMap<String, Object> module = new HashMap<>();

        module.put("package","com.hjy");
        module.put("moduleName","");
        module.put("tableName",tableInfo.get("tableName"));
        module.put("tableComment",tableInfo.get("tableComment"));
        module.put("className",StrUtil.upperFirst(tableInfo.get("tableName").toString()));
        List<Map<String, String>> maps = new ArrayList<>();
        HashMap<String, String> pkMap = new HashMap<>();
        for (Map<String, String> map : columnsInfo) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("comment",map.get("columnComment"));
            objectObjectHashMap.put("attrName",map.get("columnName"));
            objectObjectHashMap.put("columnName",map.get("columnName"));
            objectObjectHashMap.put("attrType","String");
            if (map.get("columnKey").equals("PRI")){
                pkMap.put("columnName",map.get("columnName"));
                if (map.get("EXTRA").equals("auto_increment")) {
                    pkMap.put("type","auto_increment");
                }else {
                    pkMap.put("type","");
                }
            }
            maps.add(objectObjectHashMap);
        }
        module.put("columns",maps);
        if (pkMap.size() <= 0) {
            pkMap.put("columnName",maps.get(0).get("tableName"));
            pkMap.put("type","");
        }
        module.put("pk",pkMap);
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\AuthServiceImpl.java"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();


    }

    @Test
    public void test5() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("controller.ftl");
        // 构造数据
        List<Map<String, String>> columnsInfo = sqlMapper.queryColumns("auth");
        Map<String, String> tableInfo = sqlMapper.queryTable("auth");

        // 数据
        HashMap<String, Object> module = new HashMap<>();

        module.put("package","com.hjy");
        module.put("moduleName","");
        module.put("tableName",tableInfo.get("tableName"));
        module.put("tableComment",tableInfo.get("tableComment"));
        module.put("className",StrUtil.upperFirst(tableInfo.get("tableName").toString()));
        List<Map<String, String>> maps = new ArrayList<>();
        HashMap<String, String> pkMap = new HashMap<>();
        for (Map<String, String> map : columnsInfo) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("comment",map.get("columnComment"));
            objectObjectHashMap.put("attrName",map.get("columnName"));
            objectObjectHashMap.put("columnName",map.get("columnName"));
            objectObjectHashMap.put("attrType","String");
            if (map.get("columnKey").equals("PRI")){
                pkMap.put("columnName",map.get("columnName"));
                pkMap.put("attrName",map.get("columnName"));
                pkMap.put("attrType","String");
                if (map.get("EXTRA").equals("auto_increment")) {
                    pkMap.put("type","auto_increment");
                }else {
                    pkMap.put("type","");
                }
            }
            maps.add(objectObjectHashMap);
        }
        module.put("columns",maps);
        if (pkMap.size() <= 0) {
            pkMap.put("columnName",maps.get(0).get("tableName"));
            pkMap.put("type","");
        }
        module.put("pk",pkMap);
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\AuthController.java"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();


    }

    @Test
    public void test6() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("mapperXml.ftl");
        // 构造数据
        List<Map<String, String>> columnsInfo = sqlMapper.queryColumns("auth");
        Map<String, String> tableInfo = sqlMapper.queryTable("auth");

        // 数据
        HashMap<String, Object> module = new HashMap<>();

        module.put("package","com.hjy");
        module.put("moduleName","");
        module.put("tableName",tableInfo.get("tableName"));
        module.put("tableComment",tableInfo.get("tableComment"));
        module.put("className",StrUtil.upperFirst(tableInfo.get("tableName").toString()));
        List<Map<String, String>> maps = new ArrayList<>();
        HashMap<String, String> pkMap = new HashMap<>();
        for (Map<String, String> map : columnsInfo) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("comment",map.get("columnComment"));
            objectObjectHashMap.put("attrName",map.get("columnName"));
            objectObjectHashMap.put("columnName",map.get("columnName"));
            objectObjectHashMap.put("attrType","String");
            if (map.get("columnKey").equals("PRI")){
                pkMap.put("columnName",map.get("columnName"));
                pkMap.put("attrName",map.get("columnName"));
                pkMap.put("attrType","String");
                if (map.get("EXTRA").equals("auto_increment")) {
                    pkMap.put("type","auto_increment");
                }else {
                    pkMap.put("type","");
                }
            }
            maps.add(objectObjectHashMap);
        }
        module.put("columns",maps);
        if (pkMap.size() <= 0) {
            pkMap.put("columnName",maps.get(0).get("tableName"));
            pkMap.put("type","");
        }
        module.put("pk",pkMap);
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\mapperXml.xml"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();


    }

    @Test
    public void test7() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("vueAddOrUpdate.ftl");

        // 数据
        HashMap<String, Object> module = dataUtil.getTemplateData("auth");
        System.out.println("module = " + module);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\vueAddOrUpdate.vue"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();
    }

    @Test
    public void test8() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        configuration.setEncoding(Locale.CHINESE,"UTF-8");
        System.out.println(this.getClass().getClassLoader().getResource("").getPath() + "/templates");
        configuration.setDirectoryForTemplateLoading(new File(this.getClass().getClassLoader().getResource("").getPath()+ "/templates"));
        Template template = configuration.getTemplate("router.ftl");

        // 数据
        HashMap<String, Object> module = dataUtil.getTemplateData("auth");

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\hjy\\Desktop\\router.vue"), Charset.defaultCharset());
        template.process(module,outputStreamWriter);

        outputStreamWriter.close();
    }
}
